package com.grq.framework.core.result;

import com.grq.framework.core.enums.ResultEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 数据返回类
 * @param <T>
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResultData<T> implements Serializable {

    // 返回码，0为成功，其他都是失败
    private int code;
    // 返回信息
    private String msg;
    // 返回数据
    private T data;

    /**
     * 请求成功
     * @return
     */
    public static ResultData success() {
        return build(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), null);
    }
    public static <T> ResultData<T> success(T data) {
        return build(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), data);
    }
    public static <T> ResultData<T> success(T data, String message) {
        return build(ResultEnum.SUCCESS.getCode(), message, data);
    }

    /**
     * 错误
     * @return
     */
    public static ResultData error() {
        return build(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg(), null);
    }
    public static ResultData error(String message) {
        return build(ResultEnum.ERROR.getCode(), message, null);
    }

    /**
     *  未登录
     * @return
     */
    public static ResultData notLogin(){
        return build(ResultEnum.NOT_LOGIN.getCode(), ResultEnum.NOT_LOGIN.getMsg(), null);
    }
    public static ResultData notLogin(String message) {
        return build(ResultEnum.NOT_LOGIN.getCode(), message, null);
    }

    /**
     *  无权限
     * @return
     */
    public static ResultData notPermission(){
        return build(ResultEnum.NOT_PERMISSION.getCode(), ResultEnum.NOT_PERMISSION.getMsg(), null);
    }
    public static ResultData notPermission(String message) {
        return build(ResultEnum.NOT_PERMISSION.getCode(), message, null);
    }

    /**
     *  二级认证
     * @return
     */
    public static ResultData notSafe(){
        return build(ResultEnum.PRECONDITION_FAILED.getCode(), ResultEnum.PRECONDITION_FAILED.getMsg(), null);
    }
    public static ResultData notSafe(String message) {
        return build(ResultEnum.PRECONDITION_FAILED.getCode(), message, null);
    }


    private static <T> ResultData<T> build(int code, String msg, T data) {

        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(code);
        resultData.setMsg(msg);
        resultData.setData(data);
        return resultData;
    }

}
